package cn.itsource.zcn.org.controller;

import cn.itsource.zcn.org.domain.Employee;
import cn.itsource.zcn.org.domain.Shop;
import cn.itsource.zcn.org.domain.ShopLog;
import cn.itsource.zcn.org.query.DepartmentQuery;
import cn.itsource.zcn.org.query.ShopQuery;
import cn.itsource.zcn.org.service.IEmployeeService;
import cn.itsource.zcn.org.service.IShopLogService;
import cn.itsource.zcn.org.service.IShopService;
import cn.itsource.zcn.utils.AjaxResult;
import cn.itsource.zcn.utils.QueryResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.web.bind.annotation.*;

import javax.mail.internet.MimeMessage;
import java.util.Date;
import java.util.List;


/**
 * (Shop)表控制层
 *
 * @author makejava
 * @since 2021-08-13 19:46:51
 */
@RestController
@RequestMapping("/shop")
public class ShopController {
    /**
     * 服务对象
     */
    @Autowired
    private IShopService tShopService;

    @Autowired
    private JavaMailSender javaMailSender;

    @Autowired
    private IEmployeeService employeeService;

    @Autowired
    private IShopLogService shopLogService;
    /**
     * 查询全部数据
     *
     * @return List<>
     */
    @RequestMapping("/all")
    public List<Shop> queryAll() {
        return tShopService.queryAll();
    }

    /**
     * 商家入驻
     * @return List<>
     */
    @PostMapping("/settlement")
    public AjaxResult settlement(@RequestBody Shop shop) {
        try {
            tShopService.save(shop);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        }
    }

    /**
     * 商家入驻资料修改后
     * @return List<>
     */
    @PostMapping("/updateshop")
    public AjaxResult updateshop(@RequestBody Shop shop) {
        try {
//            System.out.println(shop);
            shop.setState(0);
            tShopService.update(shop);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        }
    }

    /**
     * 入驻驳回
     * @return AjaxResult
     */
    @PutMapping("/respect")
    public AjaxResult respect(@RequestBody Shop shop){
//        System.out.println(shop);
        try {
            Shop shop1 = tShopService.queryById(shop.getId());
            shop1.setState(-1);
            tShopService.update(shop1);

            Employee employee = employeeService.queryById(shop.getAdmin_id());

            // 生成审核记录
            ShopLog shopLog = new ShopLog();
            shopLog.setAdmin_id(1L);
            shopLog.setShop_id(shop.getId());
            shopLog.setCreateDate(new Date());
            shopLog.setReason(shop.getReason());
            shopLogService.save(shopLog);

            //创建复杂邮件对象
            MimeMessage mimeMessage = javaMailSender.createMimeMessage();
            //发送复杂邮件的工具类
            MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true,"utf-8");
            helper.setFrom("2045698851@qq.com");
            helper.setSubject("您的店铺入驻审核被驳回");

            helper.setText("<h1>驳回原因："+shop.getReason()+"。点击下方链接修改资料</h1>"
                    +"<a href='http://127.0.0.1:8081/#/register2?id="+shop.getId()+"'>修改资料</a>",true);

            //收件人
            helper.setTo(employee.getEmail());

            javaMailSender.send(mimeMessage);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        }

    }

    /**
     * 入驻通过
     * @return AjaxResult
     */
    @PutMapping("/pass")
    public AjaxResult pass(@RequestBody Shop shop){
//        System.out.println(shop);
        try {
            Shop shop1 = tShopService.queryById(shop.getId());
            shop1.setState(1);
            tShopService.update(shop1);

            Employee employee = employeeService.queryById(shop.getAdmin_id());

            // 生成审核记录
            ShopLog shopLog = new ShopLog();
            shopLog.setAdmin_id(1L);
            shopLog.setShop_id(shop.getId());
            shopLog.setCreateDate(new Date());
            shopLog.setReason(shop.getReason());
            shopLogService.save(shopLog);

            //创建复杂邮件对象
            MimeMessage mimeMessage = javaMailSender.createMimeMessage();
            //发送复杂邮件的工具类
            MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true,"utf-8");
            helper.setFrom("2045698851@qq.com");
            helper.setSubject("您的店铺入驻审核已通过");

            helper.setText("<h1>您的店铺入驻审核已通过，请点击下方链接激活管理员</h1>"
                    +"<a href='http://127.0.0.1:8080/success/active?id="+employee.getId()+"'>激活</a>",true);

            //收件人
            helper.setTo(employee.getEmail());

            javaMailSender.send(mimeMessage);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        }

    }



    /**
     * 添加或者修改
     * @return AjaxResult
     */
    @PutMapping
    public AjaxResult saveOrUpdate(@RequestBody Shop shop) {
        try{
            if(shop.getId()!=null){
                tShopService.update(shop);
            }else{
                tShopService.save(shop);
            }
            return AjaxResult.me();
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        }
    }
    /**
     * 删除数据
     * @return AjaxResult
     */
    @DeleteMapping("/{id}")
    public AjaxResult delete(@PathVariable("id") Long id) {
        try {
            tShopService.delete(id);
            return AjaxResult.me();
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        }
    }

    /**
     * 查询一条数据
     * @return List<>
     */
    @GetMapping("/{id}")
    public Shop queryById(@PathVariable("id") Long id) {
        return tShopService.queryById(id);
    }


    /**
     * 查询高级+分页数据
     * @return QueryResult
     */
    @PostMapping
    public QueryResult queryPage(@RequestBody ShopQuery shopQuery) {
//        System.out.println(shopQuery);
        return tShopService.queryPage(shopQuery);
    }
    /**
     * 批量删除
     * @return AjaxResult
     */
    @PatchMapping
    public AjaxResult patchDelete(@RequestBody  Long[] ids) {
        try {
            tShopService.deleteBatch(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        }

    }

}
